package com.yulongtian.offers.day202212;

import java.util.Arrays;

/**
 * @author yulongTian
 * @create 2022-12-26 20:25
 */
public class Test06 {
    public static void main(String[] args) {
        System.out.println(longestPalindrome("aaaaacccaccccccczzAAZZ"));
    }

    public static int longestPalindrome(String s) {
        int[] chs = new int[52];
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c >= 'a' && c <= 'z') {
                chs[c - 'a']++;
            } else {
                chs[c - 'A' + 26]++;
            }
        }

        Arrays.sort(chs);

        //最小的奇数加上，其他奇数都减一
        //最小奇数是否减一
        boolean flag = false;
        int ans = 0;
        for (int i = 0; i < chs.length; i++) {
            if (chs[i] % 2 == 1) {
                if (!flag) {
                    ans += chs[i];
                    flag = true;
                } else {
                    ans += chs[i] - 1;
                }
            } else if (chs[i] != 0) {
                ans += chs[i];
            }

        }

        return ans;
    }
}
